package com.wewoo.log4e.starter.mongodb.action;

import com.wewoo.log4e.core.entity.OperationLog;
import com.wewoo.log4e.core.exception.Log4eException;
import com.wewoo.log4e.core.util.ReadLogAbstractUtil;
import lombok.Builder;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;
import java.util.stream.Collectors;
@Builder
public class ReadLogMongoDBUtil extends ReadLogAbstractUtil<OperationLog> {
    public static final Logger LOGGER = LoggerFactory.getLogger(ReadLogMongoDBUtil.class);
    @Setter
    private MongoTemplate mongoTemplate;

    @Override
    public List<String> readLogList() {
        LOGGER.info("==============================mongodb log util start read log data==============================");
        List<OperationLog> operationLogs = mongoTemplate.findAll(OperationLog.class);
        if (operationLogs.size() > 0) {
            return operationLogs.stream().map(OperationLog::getDescripition).collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public List<OperationLog> readLogList(OperationLog operationLog) {
        LOGGER.info("==============================mongodb log util start read log data==============================");
        if (null == operationLog) {
            throw new Log4eException("param operationLog is null ");
        }
        Query query = new Query(Criteria.where("relateUnique").is(operationLog.getRelateUnique()));
        return mongoTemplate.find(query,OperationLog.class,"operationLog");
    }
}
